package interview;

/**
 * @author zhangjie
 */
public class BinarySearchMax {

  public static void main(String[] args) {
    int[] nums = new int[]{1, 2, 3, 4, 3, 2, 1};
    int result = solution(nums);
    System.out.println(result);
  }

  public static int solution(int[] nums) {
    int left = 0;
    int right = nums.length - 1;
    while (left <= right) {
      int mid = left + ((right - left) >>> 1);
      if (mid - 1 >= 0 && nums[mid] > nums[mid - 1]) {
        if (mid + 1 <= nums.length - 1 && nums[mid] > nums[mid + 1]) {
          return mid;
        } else {
          left = mid + 1;
        }
      } else {
        right = mid - 1;
      }
    }
    return -1;
  }
}
